﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace AmesaExporter.ConnectionManager
{
    public class ConnectionManagerHelper
    {
        const string _connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;'";

        public OleDbConnection MyConnection { get; private set; }

        public ConnectionManagerHelper(string FileName)
        {
            MyConnection = GetConnection(FileName);
        }

        private OleDbConnection GetConnection(string FileName)
        {
            return new OleDbConnection(string.Format(_connectionString, FileName));
        }

        public OleDbDataReader GetReader(string OleDbCommand)
        {
            if (MyConnection.State != System.Data.ConnectionState.Open)
                MyConnection.Open();

            return new OleDbCommand(OleDbCommand, MyConnection).ExecuteReader();
        }

        public int GetScalar(string OleDbCommand)
        {
            if (MyConnection.State != System.Data.ConnectionState.Open)
                MyConnection.Open();

            return (int)new OleDbCommand(OleDbCommand, MyConnection).ExecuteScalar();
        }
       

    }
}
